#include"khaibao.h"
void Empty(list &l)
{
	l.pHead=l.pTail=NULL;
}
node *CreateNode(int x)
{
	node *p=new node();
	p->data=x;
	p->pNext=NULL;
	return p;
}
void Import(list &l)
{
	int n,x;
	node *p;
	Empty(l);
	cout<<"Nhap vao So pt:";cin>>n;
	for(int i=0;i<n;i++)
	{
		do
		{
			cout<<"Nhap vao pt thu "<<i<<" :";cin>>x;
		}while(x==0);
		p=CreateNode(x);
		if(l.pHead==NULL)
		{
			l.pHead=l.pTail=p;
		}
		else
			l.pTail->pNext=p;
		p->pNext=NULL;
		l.pTail=p;
	}
}
void Export(list l)
{
	node *p;
	for(p=l.pHead;p;p=p->pNext)
	{
		cout<<"\t"<<p->data;
	}
	cout<<"\n";
}
void AddHead(list &l)
{
	node *p=new node();
	if(p==NULL)
		return ;
	int x;
	cout<<"Nhap vao X:";cin>>x;
	p->data=x;
	p->pNext=NULL;
	if(l.pHead==NULL)
	{
		l.pHead=l.pTail=p;
	}
	else
	{
		p->pNext=l.pHead;
		l.pHead=p;
	}
}
void AddTail(list &l)
{
	node *p=new node();
	if(p==NULL)
		return ;
	int x;
	cout<<"Nhap vao X:";cin>>x;
	p->data=x;
	p->pNext=NULL;
	if(l.pHead==NULL)
		l.pHead=l.pTail=p;
	else
	{
		l.pTail->pNext=p;
		p->pNext=NULL;
		l.pTail=p;
	}
}
void AddBetween(list &l)
{
	int n,x;
	cout<<"Nhap N:";cin>>n;
	node *p=new node();
	node *q=new node();
	p=l.pHead;
	for(int i=1;i<n;i++)
	{
		p=p->pNext;
		if(p==NULL)
		{
			cout<<"so pt:"<<n<<endl;
			break;
		}
	}
	cout<<"Nhap pt:";cin>>x;
	q->data=x;
	q->pNext=p->pNext;
	p->pNext=q;
}
void DeleteHead(list &l)
{
	node *p=new node();
	if(l.pHead==NULL)
		cout<<" ";
	else
		p=l.pHead;
	l.pHead=p->pNext;
	free(p);
}
void DeleteTail(list &l)
{
	node *p=new node();
	node *q=new node();
	p=l.pHead;
	while(p->pNext!=NULL)
	{
		q=p;
		p=p->pNext;
	}
	q->pNext=NULL;
	free(p);
}
void DeleteBetween(list &l)
{
	node *p=new node();
	node *q=new node();
	int n;
	p=l.pHead;

	cout<<"Nhap N:";cin>>n;
	for(int i=1;i<n;i++)
	{
		q=p;
		p=p->pNext;
	}
	q->pNext=p->pNext;
	free(p);

}
